'use strict';
import BaseService from './base';
import { IDraftAttr } from '../model/draft';
const createRule = {
  authorId: {
    type: 'number',
    required: true,
  },
  articleId: {
    type: 'number',
    required: false,
  },
  content: {
    type: 'string',
    required: true,
  },
};

export default class DraftService extends BaseService {
  /**
   * @description: 获取用户的草稿
   * @param {number} userId 用户ID
   * @return:
   */
  async getDrafts(userId: number) {
    const { ctx } = this;
    return await ctx.model.Draft.findAll({ where: { authorId: userId } });
  }
  /**
   * @description: 保存
   * @param {type}
   * @return:
   */
  async doSave(payload: IDraftAttr) {
    const { ctx } = this;
    ctx.validate(createRule, payload);
    return await ctx.model.Draft.create(payload);
  }

  /**
   * @description: 编辑
   * @param {number} id 草稿ID
   * @param {object} payload 更新values
   * @return:
   */
  async doUpdate(id: number, payload: IDraftAttr) {
    const { ctx } = this;
    await ctx.model.Draft.update(payload, { where: { id } });
  }
  /**
   * @description: 删除
   * @param {number} id 草稿ID
   * @return:
   */
  // 删除
  remove(id: number, userId: number, articleId: number) {
    const { ctx } = this;
    return ctx.model.Draft.update({
      status: 0,
      articleId,
    }, { where: { id, authorId: userId } });
  }
}
